﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace order_manager
{
    //订单明细类
    class OrderItem
    {
        public string goods_Name;//商品名称
        public string Time;//交易时间
        public int price;//单价
        public int amount;//购买数量
        public int total_price;//总价

        public OrderItem(int price, int amount, string Gname)
        {
            total_price = price * amount;
            goods_Name = Gname;
            Time = DateTime.Now.ToString("yyyy-MM-dd");
            price = price;
            amount = amount;
        }
    }

    //订单类
    class Order
    {
        public string customer_Name;//顾客名称，查询条件3
        public int price_Sum = 0;//交易总额
        public int order_ID;//订单号，查询条件1
        public List<OrderItem> order_List = new List<OrderItem>();//用于储存订单明细

        public Order(int ID, string CName)
        {
            order_ID = ID;
            customer_Name = CName;
        }

        //通过订单号的唯一性判断是否重复添加订单
        public override bool Equals(object obj)
        {
            Order o = obj as Order;
            return o != null && o.order_ID == order_ID;
        }

        //添加订单明细
        public void addOrderItem(OrderItem itm)
        {
            order_List.Add(itm);
            Console.WriteLine("添加成功");
            price_Sum = price_Sum + itm.total_price;
        }

        //逐个输出订单明细
        public override string ToString()
        {
            Console.WriteLine();
            Console.WriteLine("-------------------------------");
            Console.WriteLine("订单号：" + order_ID + ' ' + "客户名：" + customer_Name);
            foreach (OrderItem a in order_List)
            {
                Console.WriteLine("商品名称：" + a.goods_Name + ' ' + "商品单价" + a.price + ' ' + "购买数量：" + a.amount + ' ' + "总价：" + a.total_price + ' ' + "交易时间：" + a.Time);
            }
            Console.WriteLine("总额：" + price_Sum);
            Console.WriteLine("-------------------------------");

            return "成功输出";
        }
    }

    //订单服务类
    class OrderService
    {
        public List<Order> order_List = new List<Order>();

        //查询订单,先写依据订单号的查询
        public bool searchOrder(int ID)
        {
            var order = from w in order_List where w.order_ID == ID select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("未查询到该订单，请检查你输入的订单号是否正确。");
                return false;
            }
            order.FirstOrDefault().ToString();
            return true;
        }

        //添加订单
        public void addOrder(Order a)
        {
            bool flag = true;
            foreach (Order order in order_List)
            {
                //订单号重复
                if (a.Equals(order))
                {
                    Console.WriteLine("订单号已存在，无法再次添加");
                    flag = false;
                    break;
                }
            }
            //如果未重复添加，则加入该订单
            if (flag == true)
            {
                order_List.Add(a);
                Console.WriteLine("添加成功！");
            }
        }

        //查询用户名
        public bool searchOrder(string name)
        {
            var order = from w in order_List where w.customer_Name == name select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("未查询到该用户，请检查你输入的用户名是否正确。");
                return false;
            }
            order.FirstOrDefault().ToString();
            return true;
        }

        //删除订单
        public void deleteOrder(int ID)
        {
            var order = from w in order_List where w.order_ID == ID select w;
            if (order.FirstOrDefault() != null)
            {
                order_List.Remove(order.FirstOrDefault());
                Console.WriteLine("该订单删除成功");
            }
            else
                Console.WriteLine("该订单不存在");
        }

        //修改订单
        public void AlterOrder(int ID)
        {
            var order = from w in order_List where w.order_ID == ID select w;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();

            Order firstOrder = new Order(0001, "zyq");
            OrderItem LOL = new OrderItem(2, 15, "LOL");
            firstOrder.addOrderItem(LOL);
            OrderItem changdan = new OrderItem(7, 50, "床单");
            firstOrder.addOrderItem(changdan);
            OrderItem cup = new OrderItem(5, 14, "杯子");
            firstOrder.addOrderItem(cup);

            Order secondOrder = new Order(0002, "fhq");
            OrderItem LOL2 = new OrderItem(5, 10, "LOL");
            secondOrder.addOrderItem(LOL2);
            OrderItem chuangdan2 = new OrderItem(4, 5, "床单");
            secondOrder.addOrderItem(chuangdan2);

            Order thirdOrder = new Order(0002, "fhq");

            service.addOrder(firstOrder);
            service.addOrder(secondOrder);
            service.addOrder(thirdOrder);

            service.searchOrder(0001);
            service.searchOrder(0002);
            service.deleteOrder(0001);
            service.searchOrder(0001);
            service.searchOrder("fhq");

            Console.ReadKey();
        }
    }
}
